<?php
namespace App\Services\Admin;

use App\Repositories\Admin\CategoryRepository;

class CategoryService
{
    private $service;

    public function __construct()
    {
        $this->service = new CategoryRepository();
    }

    public function getIndex($pid)
    {
        $models = $this->service->getParentList($pid);
        if (!count($models)) { return array(); }
        foreach ($models as $model) {
            $model->subs = $this->service->getSubList($model->id);
        }
        return $models;
    }

    public function getParents()
    {
        $models = $this->service->getParentList();
        $dataArr = array('所有');
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $dataArr[$model->id] = $model->name;
        }
        return $dataArr;
    }

    public function getSubs($id)
    {
        return $this->service->getSubList($id);
    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }
}